package com.yangff.utils;


import com.yangff.entity.vo.JwtVo;
import io.jsonwebtoken.Claims;

import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;


import java.util.Date;
import java.util.HashMap;
import java.util.Map;



public class JwtUtil {
    //com.yangff.fts
    private final static String secretKey = "Y29tLnlhbmdmZi5JbnRlbGxpZ2VudC1GdW5kLVRyYW5zYWN0aW9uIFN5c3RlbQ==";

    //五分钟
    public final static Integer SHORT_TIME = 60 * 5;
    //一小时
    public final static Integer DEFAULT_TIME = 60 * 60;

    public static String createJwtToken(String account, String type, Integer expireTime) throws Exception {
        Map<String, Object> claims = new HashMap<>();
        claims.put("account", account);
        claims.put("type", type);


        String token = Jwts.builder()
                .signWith(SignatureAlgorithm.HS256, secretKey)
                .addClaims(claims)
                .setExpiration(new Date(System.currentTimeMillis() + 1000 * expireTime))
                .compact();

        return token;

    }

    public static JwtVo checkJwtToken(String token) throws Exception {

        JwtVo jwtVo = new JwtVo();
        Claims claims = Jwts.parser()
                .setSigningKey(secretKey)
                .parseClaimsJws(token)
                .getBody();
        jwtVo.setAccount(claims.get("account", String.class));
        jwtVo.setType(claims.get("type", String.class));
        return jwtVo;
    }

}
